package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000131623795";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8G7SC8erpWmOwaDNdAqhRZrGMl1CIBKROycgFRXInwxPEYil827Ev8Qy/U0oLrFTx6XyyurY24G8i9jlt0+QhO6a2lWjEH3j/QT5beVwn0HPynRvAytQdDU4ngKWRi7TyZ9JNVbvH21hD9Nw3T6VywmrPlL3y9u8oUv9tD+Krcmx8DVaLT+myCiZuY1eJb4auFbVWrUm0EjBry57FeRxENvX5iqfATZ7txyIBpBLX2xog1dGtuti3qn+AXz3kA9dr2vpQ+TKjgMBqpClmSbJLdpxBNI0RCAbCXdACBA26gwyiHrbTc425dQxpF3FODzIHva2mulHHkFYskINcnVQhAgMBAAECggEABzrtYkcYJ2YBBOvMZZvPimlvE59RShkbk6ev13wG2j5mSRsYyyLKmbQjMEtsQiyaiz3VPC1vCYbw3qAmLvGBCEH5s3TktP0csnr4gZ2VjDY37rBZO7zQEFnNkjkgpAqBpvuUmsJb2F+28c28lSerjVCpuFbYQjl5kcw24yNELCDF4aVTzSwxwMo4f4OqqzSuqWWfdanocnRt0UqyzM5HgWeovhay/Cyp0FzUgN5c1C/SIQszcvrtwLgmXXf0BVNNVBU2gflJJz0ml3wlSYwVeWI1P6AZRKL0xAh1YI4uvEVWE6piaXUNNP3ySLy0NJBzW51IdSjaQuQMfHrv8UgXQQKBgQDpnijOTzyq2wiAxThGiVaJVE2rk5Xh68+OsuZ0Pp0nqC2dJ1t5ecf++WniFZweGtG32F9DdKzpldvmb1o2fFaTNRoYsHR1rI6os/jW0rvyWjKPDTTxPgrHY3TEmLFodMmdQzK1fmlfHCtRaD+/TeQIw8lVIc2sMRn/2uGODbmeWQKBgQDOIVor5+2MliuxEe34N1jtSRXmQ9D3WGF/tJMKqoW+X1kcEsZLJouFOU3Q36Vob7vPV+CAyiPprhAjn6woGcwYRYk7JFm8mz8kwkgONlLNt17RKHqwyU/Udabx5sasvyl+t2SHFxn37y8td9mHzEWXoF7YHlnAi458ejCXldJ7CQKBgQCaGrFyPkSXs/qs7uvLY8cIuequVqGVMPdue0aXwcHrKf5E33Rw8ktiP2aAM58TIL/clUdp+Ooi/67aF/QbYvO9EVdQl/2KrjRAxs9L7iD6olMdmCmnRTTZCBs7oDnO3NMuAOjPXj7l9XABuFHswNW+jmAd3zmL76vl4Z3OFfGuqQKBgDgju0RD+V2VtLaYFIDKyixuAEnjZHFJH9zBGF8KTDyqCUHtFxZ2m/8fuULO9mS4MaVp4JoS8678GicpoKCXTg+6OILkBpCok4z2YNuGDg07CNbcOHYXjedmV56AZNG9ipaZagak0wzElsvCXXtvtfBy3AHuWjFnkNn0CXHvexDJAoGAHZyPrez2X0UQ3eAklLafi1iZ2vXqJYBVNcCkt4dnp1/1ezFA82JrXYODCBJfdU0vtuL+CjcJxnC0xsOzNcAy8BlqgHvgqDVZXB1e+ybKWigyiFahgCiHyr0sARvBIZsyOP9eImn2CQOTYoxw7k+JOKDeEYngCtssmVKDPS9q7VI=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwWh962X7+IViKAy3TggaS2pzfVl3OtdJ6b6sjPAK+MxAeKWTVvgDY4O0p+UBPJs1nFOhcA+lY448GZ3ikHl/g/QklqbP5f9fqD42V20aqEE3Bd9vmt/ilNIwZjBa5Rm8Gu+ce0wMkWA1s1ueGqEzMrEWiPo5vJbEooz7VAJ6L7XefufEJ2hXvY18gYd46rnGYlHuxYjSmzFiz8O2fK2UxAs/yf0gtpNynjTLXKD22zNMQW6f1ENLImiC2P2uiAhR+LjXoxo/bqxDTGp89uoifxk2CtR4lDZyOoY9yAaggT4yV5/d+8D2oRgUIxnyPjZ7FyNYM6nkhI/VPSbgoitOLwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "https://458p89x530.imdo.co/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
